package data

import (
	"gitee.com/shuokeyun/order-service/internal/data/ent"
	"gitee.com/shuokeyun/order-service/internal/domain"
)

func poConvDo(order *ent.Order) *domain.Order {
	if order == nil {
		return nil
	}
	return &domain.Order{
		Id:             order.ID,
		OrderSn:        order.OrderSn,
		AppId:          order.AppID,
		Status:         order.Status,
		SettleType:     order.SettleType,
		PriceArea:      order.PriceArea,
		Brand:          order.Brand,
		TotalPrice:     order.TotalPrice,
		OrderPrice:     order.OrderPrice,
		MerchantId:     order.MerchantID,
		MerchantUserId: order.MerchantUserID,
		PayStatus:      order.PayStatus,
		PayMethod:      order.PayMethod,
		PayTime:        order.PayTime,
		RefundPrice:    order.RefundPrice,
		RefundStatus:   order.RefundStatus,
		RefundTime:     order.RefundTime,
		ReportStatus:   order.ReportStatus,
		ReportTime:     order.ReportTime,
		CreatedAt:      order.CreatedAt,
		UpdatedAt:      order.UpdatedAt,
		Ext:            extPoConvDo(order.Edges.Ext),
		Barcodes:       barcodePoConvDo(order.Edges.Barcodes),
		Originals:      originalPoConvDo(order.Edges.Originals),
		Users:          userPoConvDo(order.Edges.Users),
	}
}

func extPoConvDo(ext *ent.OrderExt) *domain.OrderExt {
	if ext == nil {
		return nil
	}
	return &domain.OrderExt{
		Id:                ext.ID,
		Remark:            ext.Remark,
		Department:        ext.Department,
		OutpatientNo:      ext.OutpatientNo,
		BedNo:             ext.BedNo,
		ClinicalDiagnosis: ext.ClinicalDiagnosis,
	}
}

func barcodePoConvDo(barcodes []*ent.OrderBarcode) []*domain.OrderBarcode {
	if len(barcodes) == 0 {
		return nil
	}
	list := make([]*domain.OrderBarcode, len(barcodes))
	for k, barcode := range barcodes {
		list[k] = &domain.OrderBarcode{
			Id:           barcode.ID,
			Barcode:      barcode.Barcode,
			SamplingTime: barcode.SamplingTime,
			Consumable:   barcode.Consumable,
			Number:       barcode.Number,
			Status:       barcode.Status,
			ReportTime:   barcode.ReportTime,
			CreatedAt:    barcode.CreatedAt,
			UpdatedAt:    barcode.UpdatedAt,
			Items:        itemPoConvDo(barcode.Edges.Items),
		}
	}
	return list
}

func originalPoConvDo(originals []*ent.OrderOriginal) []*domain.OrderOriginal {
	if len(originals) == 0 {
		return nil
	}
	list := make([]*domain.OrderOriginal, len(originals))
	for k, original := range originals {
		list[k] = &domain.OrderOriginal{
			Id:             original.ID,
			ItemCode:       original.ItemCode,
			ItemName:       original.ItemName,
			ItemCodeDetail: original.ItemCodeDetail,
			Price:          original.Price,
			MarketPrice:    original.MarketPrice,
		}
	}
	return list
}

func userPoConvDo(users []*ent.OrderUser) []*domain.OrderUser {
	if len(users) == 0 {
		return nil
	}
	list := make([]*domain.OrderUser, len(users))
	for k, user := range users {
		list[k] = &domain.OrderUser{
			Id:        user.ID,
			OrderId:   user.OrderID,
			BarcodeId: user.BarcodeID,
			UserId:    user.UserID,
			Name:      user.Name,
			Age:       user.Age,
			Sex:       user.Sex,
			Mobile:    user.Mobile,
			CardType:  user.CardType,
			CardId:    user.CardID,
			CreatedAt: user.CreatedAt,
			UpdatedAt: user.UpdatedAt,
		}
	}
	return list
}

func itemPoConvDo(items []*ent.OrderItem) []*domain.OrderItem {
	if len(items) == 0 {
		return nil
	}
	list := make([]*domain.OrderItem, len(items))
	for k, item := range items {
		list[k] = &domain.OrderItem{
			Id:           item.ID,
			ItemCode:     item.ItemCode,
			ItemName:     item.ItemName,
			Price:        item.Price,
			MarketPrice:  item.MarketPrice,
			ReportStatus: item.ReportStatus,
			ReportTime:   item.ReportTime,
			CreatedAt:    item.CreatedAt,
			UpdatedAt:    item.UpdatedAt,
		}
	}
	return list
}
